package com.tang.imgupload.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

import static org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;

/**
 * 消息确认,统一回调方法
 */
@Configuration
public class RabbitTemplateConfig implements ConfirmCallback, ReturnCallback {
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@PostConstruct
	public void init(){
		rabbitTemplate.setConfirmCallback(this);
	}


	//消息发送到交换器Exchange后触发回调
	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause){
//		System.out.println("消息唯一标识：" + correlationData);
//		System.out.println("确认结果：" + ack);
//		System.out.println("失败原因：" + cause);
	}

	// 消息从交换器发送到对应队列失败时触发
	@Override
	public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey){
		System.out.println("消息主体message:" + message);
		System.out.println("消息主体message:" + replyCode);
		System.out.println("描述：" + replyText);
		System.out.println("消息交换器exchange:" + exchange);
		System.out.println("消息路由键routing: " + routingKey);
	}
}
